/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.uac.dao.impl;

import java.util.List;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.core.hibernate.orm.QueryHints;
import com.eelpo.cmmp.core.spring.orm.jpa.BasicCommonDao;
import com.eelpo.cmmp.uac.dao.IUserManageDao;
import com.eelpo.cmmp.uac.dao.entity.TUser;
import com.eelpo.cmmp.uac.dao.vo.User;
import com.eelpo.cmmp.uac.dao.vo.enumed.AuthMode;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.cmmp.uac.dao.vo.enumed.YesOrNo;
import com.eelpo.common.query.Expression;
import com.eelpo.common.query.JpaCriteria;
import com.eelpo.common.query.Order;
import com.eelpo.common.query.Result;
import com.eelpo.common.query.criteria.Criteria;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

/**
 * <pre>
 * 用户管理数据访问具体实现
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Repository
public class UserManageDao extends BasicCommonDao implements IUserManageDao {

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#getUserById(java.lang.String)
	 */
	@Override
	public TUser getUserById(String id) throws DataAccessException {
		return find(TUser.class, id);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#getUserByAccount(java.lang.String)
	 */
	@Override
	public TUser getUserByAccount(final String account) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TUser.class);
		criteria.add(Expression.eq("account", account));
		criteria.setHint(QueryHints.HINT_CACHEABLE, true);
		List<?> users = getResultList(criteria);
		return (TUser) (users.isEmpty() ? null : users.get(0));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#checkExist(java.lang.String)
	 */
	@Override
	public boolean checkExist(String account) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TUser.class);
		criteria.result(Result.count());
		criteria.add(Expression.eq("account", account));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#checkExistByUpdate(java.lang.String
	 *      , java.lang.String)
	 */
	@Override
	public boolean checkExistByUpdate(String id, String account) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TUser.class);
		criteria.result(Result.count());
		criteria.add(Expression.ne("id", id));
		criteria.add(Expression.eq("account", account));
		return getResultCount(criteria) > 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#addUser(com.eelpo.cmmp.uac.dao
	 *      .entity.TUser)
	 */
	@Override
	public String addUser(TUser user) throws DataAccessException {
		persist(user);
		return user.getId();
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#updateUser(com.eelpo.cmmp.uac
	 *      .dao.entity.TUser)
	 */
	@Override
	public void updateUser(TUser user) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#deleteUser(java.lang.String)
	 */
	@Override
	public void deleteUser(String id) throws DataAccessException {
		// TODO Auto-generated method stub

	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#getUsersPaing(com.eelpo.cmmp.
	 *      uac.dao.vo.User, int, int)
	 */
	@Override
	public List<TUser> getUsersPaing(User user, int first, int max) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TUser.class);
		criteria.add(Expression.like("name", user.getName()));
		criteria.add(Expression.like("account", user.getAccount()));
		criteria.add(Expression.like("mobile", user.getMobile()));
		criteria.add(Expression.like("description", user.getDescription()));
		criteria.add(Expression.eq("allowInterface", YesOrNo.valueOf(user.getAllowInterface())));
		criteria.add(Expression.eq("allowWebLogin", YesOrNo.valueOf(user.getAllowWebLogin())));
		criteria.add(Expression.eq("interfaceAuthMode", AuthMode.valueOf(user.getInterfaceAuthMode())));
		criteria.add(Expression.eq("webAuthMode", AuthMode.valueOf(user.getWebAuthMode())));
		criteria.add(Expression.eq("status",Status.valueOf(user.getStatus())));
		criteria.order(Order.order(user.getSort()));
		criteria.setFirstResult(first);
		criteria.setMaxResults(max);
		return getResultList(criteria);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#getUsersPaing(java.lang.String,
	 *      com.eelpo.cmmp.uac.dao.vo.User, int, int)
	 */
	@Override
	public List<TUser> getUsersPaing(String userId, User user, int first, int max) throws DataAccessException {
		return null;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#getUsersCount(com.eelpo.cmmp.
	 *      uac.dao.vo.User)
	 */
	@Override
	public int getUsersCount(User user) throws DataAccessException {
		Criteria criteria = JpaCriteria.create(TUser.class);
		criteria.result(Result.count());
		criteria.add(Expression.like("name", user.getName()));
		criteria.add(Expression.like("account", user.getAccount()));
		criteria.add(Expression.like("mobile", user.getMobile()));
		criteria.add(Expression.like("description", user.getDescription()));
		criteria.add(Expression.eq("allowInterface", YesOrNo.valueOf(user.getAllowInterface())));
		criteria.add(Expression.eq("allowWebLogin", YesOrNo.valueOf(user.getAllowWebLogin())));
		criteria.add(Expression.eq("interfaceAuthMode", AuthMode.valueOf(user.getInterfaceAuthMode())));
		criteria.add(Expression.eq("webAuthMode", AuthMode.valueOf(user.getWebAuthMode())));
		criteria.add(Expression.eq("status",Status.valueOf(user.getStatus())));
		return getResultCount(criteria);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#getUsersCount(java.lang.String,
	 *      com.eelpo.cmmp.uac.dao.vo.User)
	 */
	@Override
	public int getUsersCount(String id, User user) throws DataAccessException {
		return 0;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.uac.dao.IUserManageDao#getSuperManager()
	 */
	@Override
	public TUser getSuperManager() throws ServiceException {
		Criteria criteria = JpaCriteria.create(TUser.class);
		criteria.add(Expression.eq("superManager", YesOrNo.YES.getValue()));
		criteria.setFirstResult(1);
		criteria.setMaxResults(1);
		criteria.setHint(QueryHints.HINT_CACHEABLE, true);
		return getSingleResult(criteria);
	}

}
